/*
 * Copyright (c) 2008, Spagettikod | Stockholm
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of the Spagettikod | Stockholm nor the names of its
 *     contributors may be used to endorse or promote products derived from
 *     this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package com.spagettikod.t437.simpledb;

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JViewport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.spagettikod.t437.T437;
import com.xerox.amazonws.sdb.Domain;

public class SimpleDBMainPanel extends JPanel
{

	private static final long serialVersionUID = -4968123732645461909L;

	private static final Log log = LogFactory.getLog(SimpleDBMainPanel.class);

	private SimpleDBJTable sdbTable;

	private JPanel queryPanel;

	private JTextArea queryTextArea;

	private JScrollPane tableScrollPane;

	public SimpleDBMainPanel()
	{
		sdbTable = new SimpleDBJTable();

		createQueryPanel();

		createTableScrollPane();

		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(8, 8, 2, 8);
		c.gridx = 0;
		c.gridy = 0;
		c.fill = GridBagConstraints.BOTH;
		// c.anchor = GridBagConstraints.CENTER;
		c.weightx = 1;
		c.weighty = 0;
		add(queryPanel, c);

		c.insets = new Insets(2, 8, 8, 8);
		c.gridy = 1;
		c.weighty = 1.0;
		c.fill = GridBagConstraints.BOTH;
		// c.anchor = GridBagConstraints.SOUTH;
		add(tableScrollPane, c);

		T437.getApplicationFrame().addComponentListener(new ComponentAdapter()
		{

			@Override
			public void componentResized(ComponentEvent arg0)
			{
				if (log.isDebugEnabled())
				{
					log.debug("Resized component: " + arg0.getComponent());
				}
				setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
				sdbTable.fetchData(queryTextArea.getText(), false);
				setCursor(Cursor.getDefaultCursor());
			}

		});
	}

	private void createQueryPanel()
	{
		queryPanel = new JPanel(new GridBagLayout());
		queryPanel.setMinimumSize(new Dimension(0, 80));
		queryPanel.setMaximumSize(new Dimension(0, 80));
		queryPanel.setPreferredSize(new Dimension(0, 80));

		queryTextArea = new JTextArea();
		JScrollPane queryAreaScrollPane = new JScrollPane(queryTextArea);

		JButton queryButton = new JButton("Filter");
		queryButton.setMnemonic(KeyEvent.VK_ENTER);
		queryButton.addActionListener(new ActionListener()
		{

			public void actionPerformed(ActionEvent arg0)
			{
				executeQuery();
			}

		});

		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(5, 0, 5, 5);
		c.anchor = GridBagConstraints.WEST;
		c.gridx = 0;
		c.gridy = 0;
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1;
		c.weighty = 1;
		queryPanel.add(queryAreaScrollPane, c);

		c.insets = new Insets(5, 5, 5, 5);
		c.anchor = GridBagConstraints.NORTHEAST;
		c.gridx = 1;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.weightx = 0;
		c.weighty = 0;
		queryPanel.add(queryButton, c);
	}

	private void createTableScrollPane()
	{
		tableScrollPane = new JScrollPane(sdbTable);

		tableScrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener()
		{

			public void adjustmentValueChanged(AdjustmentEvent e)
			{
				if (!e.getValueIsAdjusting())
				{
					if (getHorizontalScrollBarPercent() > 80)
					{
						T437.getApplicationFrame().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
						sdbTable.fetchData(queryTextArea.getText(), false);
						T437.getApplicationFrame().setCursor(Cursor.getDefaultCursor());
					}
				}
			}
		});
	}

	private int getHorizontalScrollBarPercent()
	{
		JViewport vp = tableScrollPane.getViewport();

		Float correctedMaximumHeight = new Float(vp.getViewSize().height) - new Float(vp.getViewRect().height);
		Float currentPosition = new Float(vp.getViewRect().y) / correctedMaximumHeight;
		currentPosition = currentPosition * 100;

		return Math.round(currentPosition);
	}

	public void executeQuery()
	{
		T437.getApplicationFrame().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
		sdbTable.queryData(queryTextArea.getText());
		T437.getApplicationFrame().setCursor(Cursor.getDefaultCursor());

	}

	public void loadDomain(Domain domain)
	{
		queryTextArea.setText(null);
		sdbTable.setDomain(domain);
		sdbTable.fetchData(queryTextArea.getText(), true);
	}

	public void fitColumnWidthsToContent()
	{
		sdbTable.fitColumnWidthsToContent();
	}
}
